/**
 * Created by hxpeng on 2017/3/24.
 */
(function () {
    'use strict';

    angular.module('App.pages.member.entrust')
        .controller('EntrustInfoCtrl', EntrustInfoCtrl);

    function EntrustInfoCtrl(entrustId,$scope,EntrustModel,AppUtils,EntrustService,$timeout,$state,LoginAccountAuth,$interval,$stateParams) {

        var vm = this;
        vm.loginAccount = {};
        vm.entrust = null;

        LoginAccountAuth.getAccount()
            .then(function(data){
                vm.loginAccount = data;
            });


        EntrustService.loadEntrustInfo(entrustId).then(function(data){
            $timeout(function() {
                vm.entrust = {};
                vm.entrust = data;
            }, 0);
        })

        $(".play-button").on("click",function() {
            $(".play-button").toggleClass("paused");
            $(".entrust-audio-play").toggleClass("playing");
        });

        vm.clientType = AppUtils.getClientType();
        if(vm.clientType === "ios"){
            vm.showMenuBoxType = "ios";
            var $iosActionsheet = $('#iosActionsheet');
            var $iosMask = $('#iosMask');
            var hideActionSheet = function() {
                $iosActionsheet.removeClass('weui-actionsheet_toggle');
                $iosMask.fadeOut(200);
            }
            $iosMask.on('click', hideActionSheet);
            $('#iosActionsheetCancel').on('click', hideActionSheet);
            $("#showIOSActionSheet").on("click", function(){
                $iosActionsheet.addClass('weui-actionsheet_toggle');
                $iosMask.fadeIn(200);
            });
        }else{
            vm.showMenuBoxType = "android";
            var $androidActionSheet = $('#androidActionsheet');
            var $androidMask = $androidActionSheet.find('.weui-mask');
            $("#showAndroidActionSheet").on('click', function(){
                $androidActionSheet.fadeIn(200);
                $androidMask.on('click',function () {
                    $androidActionSheet.fadeOut(200);
                });
            });
        }
        $scope.$on('$ionicView.beforeLeave', function(){
            $(".weui-mask").click();
            $("#iosActionsheetCancel").click();
        });


        $scope.deleteEntrust = function(entrustId){
            if(!AppUtils.isUndefinedOrNull(entrustId)){
                if(vm.entrust.entrustType === 'audio'){
                    $state.go("entrust.voice",{entrustId: vm.entrust.id});
                }else if(vm.entrust.entrustType === 'text'){
                    $state.go("entrust.text",{entrustId: vm.entrust.id});
                }
            }
        }


        $scope.executeEntrust = function(){
            if(!AppUtils.isUndefinedOrNull(vm.entrust.id)){
                EntrustModel.executedEntrust({entrustId:vm.entrust.id}).$promise.then(function(response){
                    if(response.stat == 'err'){
                        AppUtils.showAlert("执行失败：" + response.msg);
                    }else if(response.stat == 'success'){
                        AppUtils.showTipsTop("成功修改嘱托状态！");
                        $timeout(function(){
                            $state.go($state.current, $stateParams);
                        },1000)
                    }
                })
            }
        }


        //播放音频开始动作
        function playAudio(){
            $(".play-button").removeClass("paused");
            $(".entrust-audio-play").addClass("playing");
            if(!AppUtils.isUndefinedOrNull(vm.localAudioId)){
                wx.playVoice({
                    localId: vm.localAudioId // 需要播放的音频的本地ID，由stopRecord接口获得
                });
            }
        }
        //播放音频完成动作
        function pausedAudio(){
            $(".play-button").addClass("paused");
            $(".entrust-audio-play").removeClass("playing");
        }


        vm.isplaying = false;
        $scope.playAudio = function(){
            if(!vm.isplaying){
                var audio = $("audio")[0];
                var interval;
                var nprogress = $(".audio-nprogress")[0];
                if(!AppUtils.isUndefinedOrNull(audio)){
                    audio.addEventListener("playing", function() {
                        vm.isplaying = true;
                        playAudio();
                        var duration = Math.ceil(audio.duration*500);//总毫秒数
                        var count = duration/50;
                        var i = 100/count * 1000;
                        var s = 0;
                        interval = $interval(function(){
                            s = s + i;
                            nprogress.style.width =  s/1000 + "%";
                        }, 50, count);
                    }, false);
                    //音频可以播放完毕
                    audio.addEventListener("ended", function() {
                        vm.isplaying = false;
                        $timeout(function(){
                            pausedAudio();
                            nprogress.style.width = "0%";
                            $interval.cancel(interval);
                        },200)
                    }, false);
                    audio.play();
                }
            }

        }

    }

})();